package com.warningrc.system.i18n;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.warningrc.system.Constants;
import com.warningrc.system.lang.WarningLogger;

/**
 * 解析系统所支持语言环境<br/>
 * 该类读取{@link com.warning.system.Constants#I18N_LANGUAGE_RESOURCE
 * I18N_LANGUAGE_RESOURCE}对应的资源文件
 * 
 * @author <a href="http://weibo.com/weibowarning">王宁</a>
 * @date 2011-12-29
 * @email <a
 *        href="mailto:childe.wangning@gmail.com">childe.wangning@gmail.com</a>
 */
public class Languages {
	/**
	 * 
	 */
	private static Languages singleton;
	private static WarningLogger log = WarningLogger
			.getWarningLogger(Languages.class);
	private Map<String, Locale> locales;
	private Locale defaultLocale;

	/**
	 * 获取 {@link Languages Languages} 实例
	 * <p/>
	 * 当 Constants.{@link com.warningrc.system.Constants#DEBUG DEBUG} 为true时
	 * 该方法会读取{@link com.warningrc.system.Constants#I18N_LANGUAGE_RESOURCE
	 * I18N_LANGUAGE_RESOURCE}对应的资源文件并实例化一个{@link Languages Languages}对象返回，否则返回
	 * {@link Languages Languages}的单例对象。
	 * 
	 * @return
	 */
	public static Languages getInstance() {
		if (Constants.DEBUG) {
			log
					.debug("com.warningrc.system.Constants.DEBUG = true,Instantiation Languages");
			singleton = new Languages(new Control(0));
		} else if (singleton == null) {
			log
					.debug("com.warningrc.system.Constants.DEBUG = false, Languages is singleton pattern");
			singleton = new Languages(new Control(
					Control.TTL_NO_EXPIRATION_CONTROL));
		}
		return singleton;
	}

	public Locale getDefaultLocale() {
		return defaultLocale;
	}

	@SuppressWarnings("static-access")
	private Languages(Control control) {
		locales = new HashMap<String, Locale>();
		ResourceBundle rb = ResourceBundle.getBundle(
				Constants.I18N_LANGUAGE_RESOURCE, control);
		log.debug("Instantiation java.util.ResourceBundle from "
				+ Constants.I18N_LANGUAGE_RESOURCE + ".properties");
		String[] langs = rb.getString(Constants.I18N_LANGUAGES).split(",");
		for (String lang : langs) {
			String text = rb.getString(Constants.MESSAGEFORMAT.format(
					Constants.I18N_LANGUAGE_TEXT, lang));
			Locale locale = getLocale(rb.getString(Constants.MESSAGEFORMAT
					.format(Constants.I18N_LANGUAGE_LOCALE, lang)));
			log.debug("Instantiation java.util.Locale[" + locale + "] text["
					+ text + "]");
			locales.put(text, locale);
		}
		String defaultLang = rb.getString(Constants.I18N_LANGUAGE_DEFAULT);
		defaultLocale = getLocale(rb.getString(Constants.MESSAGEFORMAT.format(
				Constants.I18N_LANGUAGE_LOCALE, defaultLang)));
		log.debug("System default language is " + defaultLocale);
	}

	public Map<String, Locale> getLocales() {
		return locales;
	}

	private static Locale getLocale(String key) {
		return LocalizedTextUtil.localeFromString(key, null);
	}

	private static class Control extends ResourceBundle.Control {
		private long timeToLive;

		public Control(long timeToLive) {
			this.timeToLive = timeToLive;
		}

		@Override
		public long getTimeToLive(String baseName, Locale locale) {
			return timeToLive;
		}
	}
}
